package handler

import (
	"bytes"
	"github.com/BSNDA/PCNGateway-Go-SDK/errno"
	"github.com/BSNDA/PCNGateway-Go-SDK/model"
	"github.com/gin-gonic/gin"
	"io"
	"io/ioutil"
	"log"
	"fmt"
)

//用户地址上链
func SetUserLocation(c *gin.Context)  {
	buf := &bytes.Buffer{}
	tea := io.TeeReader(c.Request.Body, buf)
	body, err := ioutil.ReadAll(tea)
	if err!=nil{
		log.Fatal(err.Error())}
	data:=string(body)
	fmt.Println(body)
	//上链函数
	message,err:=model.SetLocation(data)
	if err != nil {
		SendResponse(c, errno.ErrDatabase, nil)
		return
	}
	if (message=="SUCCESS"){
		var suc string="用户地址上链成功！"
		SendResponse(c, nil, suc)} else {
		var faild string="上链失败！"
		SendResponse(c, nil, faild)}
}



//通过订单号查询用户地址
func GetUserLocation(c *gin.Context)  {
	var orderid model.QueryLocation
	err:=c.BindJSON(&orderid) //json绑定获取参数
	fmt.Println(orderid)
	if err != nil {
		SendResponse(c, errno.ErrDatabase, nil)
		return
	}
	message,err:=model.GetLocation(orderid.OrderId)
	if err != nil {
		SendResponse(c, errno.ErrDatabase, nil)
		return
	}
	fmt.Println(message)
	SendResponse(c, nil, message)
}